package com.shiikong_memory.yez.Tool;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ListStore {
    public static List<String> readList(String url) throws IOException {
        File file = new File(url);
        if (!file.exists()) {
            file.createNewFile();
            return new ArrayList<>();
        } else {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader br = new BufferedReader(inputStreamReader);

            // 将文件读到 List中
            String line = null;
            List<String> lines = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            return lines;
        }
    }

    public static void saveList(String url,List<String> list) throws IOException {
        File file = new File(url);
        // 判断文件是否存在
        if (!file.exists()) {
            file.createNewFile();
        }
        // 遍历写入
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (String lineL : list) {
            bw.write(lineL+"\r\n");
        }
        bw.flush();
        bw.close();
    }

    public static void addSaveList(String url, String data) throws IOException {
        File file = new File(url);
        // 判断文件是否存在
        if (!file.exists()) {
            file.createNewFile();
        }
        // 遍历写入
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(data+"\r\n");
        bw.flush();
        bw.close();
    }
}
